package com.andaily.web.controller.mobile.log;

import com.andaily.domain.dto.log.LogTypeDto;
import com.andaily.domain.dto.log.LogTypeOverviewDto;
import com.andaily.service.LogService;
import com.andaily.web.controller.validator.LogTypeValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/log/type")
public class LogTypeController {

    @Autowired
    private LogService logService;

    private LogTypeValidator logTypeValidator = new LogTypeValidator();

    @RequestMapping("overview")
    public String overview(LogTypeOverviewDto overviewDto, Model model) throws Exception {
        overviewDto = logService.loadLogTypeOverview(overviewDto);
        model.addAttribute("overviewDto", overviewDto);
        return "log/log_type_overview";
    }

    @RequestMapping("overview/more")
    public String more(LogTypeOverviewDto overviewDto, Model model) throws Exception {
        overviewDto = logService.loadLogTypeOverview(overviewDto);
        model.addAttribute("overviewDto", overviewDto);
        return "log/log_type_overview_more";
    }

    @RequestMapping("details/{guid}")
    public String details(@PathVariable() String guid, Model model) throws Exception {
        LogTypeDto logTypeDto = logService.loadLogTypeDetails(guid);
        model.addAttribute("logType", logTypeDto);
        return "log/log_type_details";
    }

    @RequestMapping("delete_confirm/{guid}")
    public String deleteConfirm(@PathVariable() String guid, Model model) throws Exception {
        model.addAttribute("guid", guid);
        return "log/log_type_delete_confirm";
    }

    @RequestMapping("delete/{guid}")
    public String delete(@PathVariable() String guid) throws Exception {
        logService.deleteLogType(guid);
        return "redirect:../overview";
    }

    @RequestMapping("default_type/{guid}")
    public String defaultType(@PathVariable() String guid) {
        logService.setDefaultLogType(guid);
        return "redirect:../overview";
    }


    @RequestMapping(value = "form", method = RequestMethod.GET)
    public String form(String guid, Model model) throws Exception {
        LogTypeDto logTypeDto = logService.loadLogType(guid);
        model.addAttribute("logType", logTypeDto);
        return "log/log_type_form";
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submit(@ModelAttribute LogTypeDto logTypeDto, BindingResult result) throws Exception {
        logTypeValidator.validate(logTypeDto, result);
        if (result.hasErrors()) {
            return "log/log_type_form";
        }
        logService.persistLogTypeDto(logTypeDto);
        return "redirect:overview";
    }
}
